<template>
  <!-- 一条文章单元格 -->
  <van-cell>
    <!-- 标题区域的插槽 -->
    <template #title>
      <div class="title-box">
        <!-- 标题 -->
        <span>{{ articleObj.title}}</span>
        <img class="thumb" :src="articleObj.cover.images[0]" alt="" v-if="articleObj.cover.type === 1">
      </div>
      <div class="thumb-box" v-if="articleObj.cover.type > 1">
        <img v-for="(urlStr,index) in articleObj.cover.images" :key="index" :src="urlStr" alt="" class="thumb">
      </div>
    </template>
    <!-- label 区域的插槽 -->
    <template #label>
      <div class="label-box">
        <div>
          <span>{{ articleObj.aut_id }}</span>
          <span>{{articleObj.comm_count}}评论</span>
          <span>{{ formatDate(articleObj.pubdate) }}</span>
        </div>
        <!-- 反馈按钮 -->
        <van-icon name="cross" @click.stop="btnFn(articleObj.aut_id)" v-if="isShow"/>
      </div>
    </template>
  </van-cell>
</template>

<script>
import dayjs from 'dayjs'
import 'dayjs/locale/zh-cn'
import * as relativeTime from 'dayjs/plugin/relativeTime'
import { mapMutations } from 'vuex' // use locale
dayjs.extend(relativeTime) // import locale
dayjs.locale('zh-cn')
export default {
  props: {
    articleObj: Object,
    isShow: {
      type: Boolean,
      default: true
    }
  },
  methods: {
    ...mapMutations(['changeActionArticleMutation']),
    ...mapMutations(['setActionArtIdMutations']),
    formatDate (dateStr) {
      var a = dayjs()
      var b = dayjs(dateStr)
      const c = a.to(b) // in 2 years
      // console.log(c)
      return c
    },
    btnFn (id) {
      this.changeActionArticleMutation(true)
      this.setActionArtIdMutations(id)
    }
  }

}
</script>

<style scoped lang="less">
/* 标题样式 */
.title-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

/* label描述样式 */
.label-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* 文章信息span */
.label-box span {
  margin: 0 3px;
  &:first-child {
    margin-left: 0;
  }
}

/* 图片的样式, 矩形黄金比例：0.618 */
.thumb {
  width: 113px;
  height: 70px;
  background-color: #f8f8f8;
  object-fit: cover;
}

/* 三图, 图片容器 */
.thumb-box {
  display: flex;
  justify-content: space-between;
}
</style>
